package com.gqb.tif2png;
import java.io.*;  
import com.lowagie.text.*; 
import com.lowagie.text.pdf.*; 
import com.lowagie.text.pdf.codec.*; 
 
public class Tifftopdf {
	 public static void main(String[] args) { 
	     if (args.length < 1){ 
	       System.out.println("Usage: Tiff2Pdf file1.tif [file2.tif  fileN.tif]"); 
	       System.exit(1); 
	     } 
	  
	     String tiff, pdf; 
	     for (int i = 0; i < args.length; i++){ 
	       tiff = "d:\\1.tif"; 
	       pdf = tiff.substring(0, tiff.lastIndexOf('.') + 1) + "pdf"; 
	       Document document = new Document(PageSize.A4, 0, 0, 0, 0); 
	       int pages = 0, comps = 0; 
	       try{ 
	         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf)); 
	         document.open(); 
	         PdfContentByte cb = writer.getDirectContent(); 
	         RandomAccessFileOrArray ra = null; 
	         try{ 
	           ra = new RandomAccessFileOrArray(tiff); 
	           comps = TiffImage.getNumberOfPages(ra); 
	         } catch (Throwable e){ 
	           System.out.println("Exception in " + tiff + " " + e.getMessage()); 
	           continue; 
	         } 
	          
	         System.out.println("Processing: " + tiff); 
	         for (int c = 0; c < comps; ++c){ 
	           try{ 
	             Image img = TiffImage.getTiffImage(ra, c + 1); 
	             if (img != null){ 
	               System.out.println("page " + (c + 1)); 
	               if(img.getWidth()>img.getHeight()){
	            	   img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY()); 
	            	   //document.setPageSize(PageSize.A3.rotate());
	               }else {
	            	   img.scalePercent(7200f / img.getDpiX()+4, 7200f / img.getDpiY()+4); 
	            	  // document.setPageSize(PageSize.A4);
	               }
	               cb.addImage(img); 
	               //document.newPage(); 
	               ++pages; 
	             } 
	           } catch (Throwable e){ 
	             System.out.println("Exception " + tiff + " page " + (c + 1) + " " + e.getMessage()); 
	           } 
	         } 
	         ra.close(); 
	         document.close(); 
	       } catch (Throwable e){ e.printStackTrace(); } 
	  
	       System.out.println("done..."); 
	        
	     } 
	   } 
}
